<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 财付通支付响应处理 (即时到账)
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: tenpay.php 199 2012-05-09 08:32:18Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Payment_Respond_Tenpay extends Payment_Respond
{

    /**
     * 分析返回的结果
     *
     * @param  array  $params
     * @return array
     */
    public function parse(array $params)
    {
        if (isset($params['total_fee']))
        {
            $params['total_fee'] = number_format($params['total_fee'] / 100, 2, '.', '');
        }

        return parent::parse($params);
    }

    /**
     * 获取订单号
     *
     * @return string
     */
    public function get_id()
    {
        return $this->get('sp_billno');
    }

    /**
     * 获取支付交易号
     *
     * @return string
     */
    public function get_payment_no()
    {
        return $this->get('transaction_id');
    }

    /**
     * 获取数据签名
     *
     * @return string
     */
    public function get_sign()
    {
        return $this->get('sign');
    }

    /**
     * 获取订单总金额
     *
     * @return float
     */
    public function get_amount()
    {
        return floatval($this->get('total_fee'));
    }

    /**
     * 获取付款时间
     *
     * @return int
     */
    public function get_time()
    {
        return $this->get('pay_time');
    }

    /**
     * 获取支付状态
     *
     * @return int
     */
    public function get_status()
    {
        return ($this->get('pay_result', 1) == 0) ? Payment::STAT_PAID : Payment::STAT_FAILED;
    }

    /**
     * 验证数字签名
     *
     * @throws Exception
     */
    protected function _verify()
    {
        extract(empty($_GET) ? $_POST : $_GET);

        $attach = isset($attach) ? $attach : '';
        $sign  = "cmdno=$cmdno&pay_result=$pay_result&date=$date"
               . "&transaction_id=$transaction_id&sp_billno=$sp_billno"
               . "&total_fee=$total_fee&fee_type=$fee_type&attach=$attach"
               . "&key=" . $this->_key;
        $sign = strtoupper(md5($sign));

        if ($sign != $this->get_sign())
        {
            throw new Payment_Exception('数字签名认证失败');
        }
    }

}